package smartcounters.androidclient.WebServices;

import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import smartcounters.androidclient.model.eOperationsEnum;

import android.os.Handler;
import android.os.Message;

public class WebMethodAsyncInvoker implements Runnable {
	private HttpTransportSE m_HttpTransportSE;
	private String m_GetSoapAction;
	private SoapSerializationEnvelope m_Envelope;
	private Handler m_Handler;
	private eOperationsEnum m_ReturnType;
	
	public WebMethodAsyncInvoker(HttpTransportSE i_HttpTransportSE, String i_GetSoapAction, SoapSerializationEnvelope i_Envelope, Handler i_Handler, eOperationsEnum i_ReturnType) {
		m_HttpTransportSE = i_HttpTransportSE;
		m_GetSoapAction = i_GetSoapAction;
		m_Envelope = i_Envelope;
		m_Handler = i_Handler;
		m_ReturnType = i_ReturnType;
	}

	public void run() {
		try {
			m_HttpTransportSE.call(m_GetSoapAction, m_Envelope);
			
			Message message = new Message();
			message.what = m_ReturnType.ordinal();
			message.obj = m_Envelope.bodyIn;
			m_Handler.sendMessage(message);
		} catch (Exception e) {
			e.printStackTrace();
			
			Message message = new Message();
			message.obj = new ConnectionError();
			m_Handler.sendMessage(message);
		}
	}
}